package com.hyq.market.myapplication.test.wu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class JsounpTest5 {

//    p06 qingchun 67  未保存
//    05  meitui 46

    private static final  String BaseUeURL="http://m.58jo.com/p07/list_233.html";
    private static final String BaseUrl="http://m.58jo.com";
    private static final String ParentUrl=BaseUrl+"/html/part/index16_19.html";
    private static final String ParentUrl_Auto=BaseUrl+"/html/part/index16_";
    private static final String BaseFilePath="E:"+ File.separator+"img"+
            File.separator+"linglei08"+File.separator;
    private static String dirPath;
    static  ExecutorService threadPool = Executors.newFixedThreadPool(30);
    public static void main(String[] args) throws Exception {
        for (int i=5 ; i >0; i--) {
            final int finalI = i;
            threadPool.execute(new Runnable() {
                public void run() {
                        try {
                            String sdf="http://m.58jo.com/p08/list_"+ finalI +".html";
                            dolist(sdf);
                            Thread.sleep(5*1000L);
                        } catch (IOException e) {
                            e.printStackTrace();
                            try {
                                dolist("http://m.58jo.com/p08/list_"+ finalI +".html");

                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                }
            });
        }

    }

    private static  void dolist(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        Elements images = doc.select("div.typelist ul >li:first-of-type");
        for (final Element image : images) {
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        String date =image.parent().select("li:last-of-type > font").text();
                        dirPath=BaseFilePath+date+
                                File.separator+
                                image.select("a").text()
                                +File.separator;
                        dirIsExists(dirPath);
                        parseUrl(BaseUrl+image.select("a").attr("href"),dirPath);
                        System.out.println("正在解析网页#####: " + image.select("a").attr("href"));
                    } catch (Exception e) {
                        e.printStackTrace();
                        try {
                            parseUrl(BaseUrl+image.select("a").attr("href"),dirPath);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            });

        }
    }

    /**********
     * 具体网页解析
     * @param url
     * @throws IOException
     */
    private static void parseUrl(String url,String relateFile) throws Exception {
            Document doc = Jsoup.connect(url).get();
            Elements images = doc.select("#view1> img");
            for (int i = 0; i < images.size(); i++) {
                Element image=images.get(i);
                String file=relateFile+ i +".jpg";
                savePic(image.attr("src"),file);

            }
    }






    public static void savePic(final String imgUrl, final String filePath)  {
       threadPool.execute(new Runnable() {
           @Override
           public void run() {
               try {
                   URL url = new URL(imgUrl);
                   HttpURLConnection conn = null;
                   conn = (HttpURLConnection) url.
                           openConnection();
                   conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                   conn.setRequestMethod("GET");
                   conn.setConnectTimeout(5 * 1000);
                   conn.setDoInput(true);
                   conn.setUseCaches(false);
                   InputStream inStream = conn.getInputStream();
                   byte[] data = readInputStream(inStream);
                   File imageFile = new File(filePath);
                   FileOutputStream outStream = new FileOutputStream(imageFile);
                   outStream.write(data);
                   outStream.close();
                   System.out.println("正在保存文件:"+filePath);
               } catch (IOException e) {
                   e.printStackTrace();
               } catch (Exception e) {
                   e.printStackTrace();
               }

           }
       });

    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024*1024*10];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }


    public static boolean dirIsExists(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            System.out.println("创建目录" + destDirName + "失败，目标目录已经存在");
            return true;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            System.out.println("创建目录" + destDirName + "成功！");
            return true;
        } else {
            System.out.println("创建目录" + destDirName + "失败！");
            return false;
        }
    }











}
